package com.cleaner.base.activities

import android.os.Bundle
import android.view.LayoutInflater
import androidx.viewbinding.ViewBinding
import java.lang.reflect.ParameterizedType

abstract class ViewBindingActivity<T : ViewBinding> : BaseActivity() {

    private lateinit var _binding: T
    protected val binding get() = _binding

    protected abstract fun onInit(savedInstanceState: Bundle?)

    @Suppress("UNCHECKED_CAST")
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        val type = javaClass.genericSuperclass as ParameterizedType
        val cls = type.actualTypeArguments[0] as Class<*>
        val inflateMethod = cls.getDeclaredMethod("inflate", LayoutInflater::class.java)
        _binding = inflateMethod.invoke(null, layoutInflater) as T
        setContentView(_binding.root)

        onInit(savedInstanceState)
    }
}